package com.example.ayniu.treadmill;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.widget.Toast;

import com.example.ayniu.treadmill.base.BaseActivity;
import com.example.ayniu.treadmill.util.UserInfoUtil;
import com.example.ayniu.treadmill.wxapi.WXEntryActivity;

public class WelcomeActivity extends BaseActivity {

    String openId = new String();
    String phone = new String();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        checkWXAndPhone();//检查是否已经进行微信授权及手机验证

    }

    /*查找本地是否已经有微信及用户手机号码信息，避免重复验证*/
    public void checkWXAndPhone(){
        try {
            openId = UserInfoUtil.getLocalPara("USER_OPENID");
            phone = UserInfoUtil.getLocalPara("USER_PHONE");
            if(openId.length() == 0){
                ToWXEntryActivity();//无openId，跳转到WXEntryActivity进入登录验证
            }else if(phone.length() == 0){
                ToSetPhoneActivity();//无绑定号码，跳转到SetPhoneActivity进入手机验证
            }else{
                ToMainActivity(); //跳转到MainActivity
                Log.i("checkWX", "openId had");
            }
        }catch (Exception e){
            Toast.makeText(WelcomeActivity.this,"用户获取失败，请重新登录",Toast.LENGTH_LONG).show();
            ToWXEntryActivity();
        }
    }

    /*淡出淡入动画实现页面跳转到MainActivity*/
    private void ToMainActivity(){
        new CountDownTimer(1000, 500) {
            @Override
            public void onTick(long millisUntilFinished) {
            }
            @Override
            public void onFinish() {
                Intent intent = new Intent();
                intent.setClass(WelcomeActivity.this, MainActivity.class);
                startActivity(intent);
                WelcomeActivity.this.overridePendingTransition(R.anim.anim_welcome_in, R.anim.anim_welcome_out);
                finish();
            }
        }.start();
    }

    /*淡出淡入动画实现页面跳转到WXEntryActivity*/
    private void ToWXEntryActivity(){
        new CountDownTimer(1000, 500) {
            @Override
            public void onTick(long millisUntilFinished) {
            }
            @Override
            public void onFinish() {
                Intent intent = new Intent();
                intent.setClass(WelcomeActivity.this, WXEntryActivity.class);
                startActivity(intent);
                WelcomeActivity.this.overridePendingTransition(R.anim.anim_welcome_in, R.anim.anim_welcome_out);
                finish();
            }
        }.start();
    }

    /*淡出淡入动画实现页面跳转到WXEntryActivity*/
    private void ToSetPhoneActivity(){
        new CountDownTimer(1000, 500) {
            @Override
            public void onTick(long millisUntilFinished) {
            }
            @Override
            public void onFinish() {
                Intent intent = new Intent();
                intent.setClass(WelcomeActivity.this, SetPhoneActivity.class);
                startActivity(intent);
                WelcomeActivity.this.overridePendingTransition(R.anim.anim_welcome_in, R.anim.anim_welcome_out);
                finish();
            }
        }.start();
    }

}
